package com.matt.controller.web.courseV2;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dotflat.check.domain.Card;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.courseV2.Ability;
import com.matt.domain.courseV2.VendorCourse;
import com.matt.domain.courseV2.VendorCoursePackage;
import com.matt.domain.courseV2.VendorCourseRel;
import com.matt.domain.courseV2.po.CourseDetailsPO;
import com.matt.domain.courseV2.po.CourseListPO;
import com.matt.domain.courseV2.po.VendorCoursePackagePO;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.utils.CopyUtils;
import com.matt.foundation.utils.ExcelOperateUtil;
import com.matt.repository.courseV2.AbilityRepository;
import com.matt.service.courseV2.AbilityService;
import com.matt.service.courseV2.VendorCoursePackageService;
import com.matt.service.courseV2.VendorCourseService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

import static com.matt.foundation.constant.RequestConstant.TICKET;


@RestController
@RequestMapping("/api/vendor_course")
@Slf4j
@Api(tags = "教学 - 课程管理")
public class VendorCourseController {

    @Autowired
    private VendorCourseService courseService;

    @Autowired
    protected MemVerificationCache cache;

    @Autowired
    private VendorCoursePackageService vendorCoursePackageService;


    @ApiOperation(value = "获取课程详情")
    @PostMapping("/get")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "课程ID", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 000000, message = "successful", response = CourseDetailsPO.class),
    })
    public StatusResponse getVendorCourse(String id) {
        VendorCourse vendorCourse = courseService.getVendorCourse(id);
        CourseDetailsPO courseDetailsPO = new CourseDetailsPO();
        CopyUtils.copyProperties(vendorCourse, courseDetailsPO);
        return StatusResponse.success(courseDetailsPO);
    }

    ;


    @ApiOperation(value = "添加课程")
    @PostMapping("/add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "title", value = "标题", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "author", value = "作者", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "content", value = "内容", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "attachments", value = "附件", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "tools", value = "工具", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "persistentTemplateId", value = "模板ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "indexInPackage", value = "包索引", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "tags", value = "标签", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "dimensions", value = "尺寸", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "guideVideo", value = "视频", required = true, dataType = "string"),
    })
    public StatusResponse addVendorCourse(String title, String author, String content, String attachments, String tools, String persistentTemplateId, int indexInPackage, String tags, String dimensions, String guideVideo) {
        return StatusResponse.success(courseService.addVendorCourse(title, author, content, attachments, tools, persistentTemplateId, indexInPackage, tags, dimensions, guideVideo));
    }

    ;

    @ApiOperation(value = "获取列表")
    @PostMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name", value = "标题", required = false, dataType = "string")
    })
    public StatusResponse getVendorList(String name, PagerQuery pagerQuery) {
        return StatusResponse.success(courseService.getVendorList(name, pagerQuery));
    }

    @ApiOperation(value = "依据课程包Id查询课程详情列表")
    @PostMapping("/packageIdByCourseList")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "packageId", value = "课程包ID", required = true, dataType = "string"),
    })
    public StatusResponse packageIdByCourseList(String packageId, PagerQuery pagerQuery) {
        return StatusResponse.success(courseService.packageIdByCourseList(packageId, pagerQuery));
    }

    @ApiOperation(value = "查询课程列表")
    @PostMapping("/getList")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "packageId", value = "课程包ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "title", value = "标题", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "sort", value = "分类字典ID", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "age", value = "年龄字典ID", required = false, dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 000000, message = "successful", response = CourseListPO.class),
    })
    public StatusResponse getList(String packageId, String title, String sort, String age, PagerQuery pagerQuery) {
        Map map = courseService.list(packageId, title, sort, age, pagerQuery);
        return StatusResponse.success(map);
    }

    @ApiOperation(value = "导入",httpMethod = "POST")
    @RequestMapping(value = "/import")
    @ResponseBody
    public StatusResponse importTemplate(@RequestHeader(TICKET) String ticket,
                                                 @RequestParam MultipartFile file) throws Exception{
        courseService.importTemplate(file);
        return StatusResponse.success("成功");
    }

    @ApiOperation(value = "添加 - 排课课程关联")
    @PostMapping("/add_schedule_rel")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "vendorCourseId", value = "课程ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "scheduleRecordId", value = "排课记录ID", required = true, dataType = "string")
    })
    public StatusResponse addScheduleRel(String vendorCourseId,String scheduleRecordId) {
        String result = courseService.addScheduleRel(vendorCourseId,scheduleRecordId);
        return StatusResponse.success(result);
    }


    @ApiOperation(value = "查询 - 班级月龄课程")
    @PostMapping("/get_class_vendor")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "classId", value = "班级ID", required = true, dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 000000, message = "successful", response = VendorCourse.class),
    })
    public StatusResponse getClassVendor(String classId, PagerQuery pagerQuery) {
        Page<VendorCourse> list = courseService.getClassVendor(classId,pagerQuery);
        return StatusResponse.success(list);
    }
}
